/**
 * MIT License
 * 
 * Copyright (c) 2024 - present @ ebraid
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */


#include <stdarg.h>
#include <kernel/syslog.h>
#include <drivers/uart.h>
#include <string.h>
#include <stdio.h>


static struct uart_device *syslog_dev = NULL;

#ifdef CONFIG_OS
#include <os/mutex.h>
static struct mutex syslog_mutex = {NULL};
#endif


/**
 * @brief ebraid klog printing function, used to print debugging information. Note that this function 
 *        should only be called in debugging mode, otherwise it may affect system real-time 
 *        performance due to long execution time
 * 
 * @param level:  log level, such as, INFO, USER...
 * @param format:  log content
 * 
 * @return none
*/
void syslog(const char *level, const char * format, ...)
{
    char buffer[256];
    int pref_size = strlen(level);

    strcpy(buffer, level);

    va_list va;
    va_start(va, format);
    vsnprintf(buffer + pref_size, sizeof(buffer), format, va);
    va_end(va);

    if(syslog_dev) {

#ifdef CONFIG_OS
    mutex_lock(&syslog_mutex);
#endif

    uart_put_string(syslog_dev, buffer);
    uart_put_string(syslog_dev, "\r\n");

#ifdef CONFIG_OS
    mutex_unlock(&syslog_mutex);
#endif

    }
}


/**
 * @brief ebraid assert handler, used to handle assertions
 * 
 * @param file:  file name
 * @param func:  function name
 * @param line:  line number
 * 
 * @return none
*/
void eb_assert_handler(const char *file, const char *func, int line)
{
    syslog(SYSASSERT_FLAG, "file: %s, function: %s, line: %d", file, func, line);
    while(1) {

    };
}


/**
 * @brief binding serial port devices to system logs
 *        In general, it is necessary to register a serial device and use this function to bind system logs
 * 
 * @param dev: uart device
 */
void syslog_bind_uart_device(struct uart_device *dev)
{
    syslog_dev = dev;
}


/**
 * @brief get serial port devices for system logs
 * 
 * @param none
 */
struct uart_device* syslog_device(void)
{
    return syslog_dev;
}


/**
 * @brief system log initialization
 * @param none
 * @return int, 0 on success, -1 on failure
 */
#ifdef CONFIG_OS
int syslog_init(void)
{
    mutex_init(&syslog_mutex);
    return 0;
}

core_initcall(syslog_init);
#endif